package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.OilStation;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.Office;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOilStationClientService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class OilStationClientService extends CommonService<OilStation> implements IOilStationClientService{
	@Autowired
	private IOfficeService iOfficeService;

	private final String listUrl = "/oilStation/list";
	private final String updateUrl = "/oilStation/update";
	
	@Override
	public void requestOilstationInf() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.OIL_STATION_NUM);
	}
	@Override
	protected Object dataHandle(OilStation oilStation) throws Exception {
		QueryWrapper<Office> entity = new QueryWrapper<Office>();
		Office info = null;
		if(StringUtils.isBlank(oilStation.getId())) {
			entity.eq("CODE", StringUtils.trim(oilStation.getCode()));
			entity.eq("TYPE", InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_GAS_STATION);
			info = iOfficeService.getOne(entity);
		}else {
			info = iOfficeService.getById(oilStation.getId());
		}
		
		boolean isInsert = false;
		if(info == null) {
			info = new Office();
			isInsert = true;
		}
		BeanUtil.copyProperties(oilStation, info, true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
		info.setType(InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_GAS_STATION);
		info.setCode(StringUtils.trim(oilStation.getCode()));
		info.setName(StringUtils.trim(oilStation.getName()));
		info.setFullName(StringUtils.trim(oilStation.getFullName()));
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);

		Date nowTime = DateUtils.getCubaDate(Calendar.getInstance().getTime());
		if(isInsert) {
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setParentId(InfStatus.LSMS_OFFICE_PARENT.ROOT);
			info.setParentIds(InfStatus.LSMS_OFFICE_PARENT.ROOT+InfStatus.SEPARATOR_CHAR);
			info.setCreateDate(nowTime);
		}
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(nowTime);
		
		if(isInsert) {
			iOfficeService.save(info);
		}else {
			iOfficeService.updateById(info);
		}
		
		return info;
	}
	
	
	@Override
	protected List<OilStation> feadBackDataHandle(Object obj, OilStation oilStation, List<OilStation> feadBackDataLists) throws Exception {
		Office info = (Office) obj;
		OilStation tmpBack = new OilStation();
		tmpBack.setId(info.getId());
		tmpBack.setCode(oilStation.getCode());
		tmpBack.setDataInputDatetime(oilStation.getDataInputDatetime());
		tmpBack.setLsmsUpdate(BigDecimal.valueOf(InfStatus.LSMS_UPDATE_EXTRACT_YES));
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
	
}
